<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019-10-18
 * Time: 13:59
 */

namespace app\manage\admin;
use app\admin\controller\Admin;
use app\common\builder\ZBuilder; // 引入ZBuilder
use app\common\model\ProcessType as ProcessTypeModel;
use think\Db;

class ProcessType extends Admin
{
    //投票列表
    public function lists()
    {
        // 读取数据
        $map = $this->getMap();
        // 排序
        $order = $this->getOrder();
        // 数据列表
        $data_list = ProcessTypeModel::where($map)->order($order)->paginate();
        // 分页数据
        $page = $data_list->render();
        $btn_access1 = [
            'title' => '编辑',
            'icon'  => 'fa fa-fw fa-pencil',
            'class' => 'btn btn-xs btn-default',
            'href'  => url('edit', ['id' => '__id__']),
        ];
        $btn_access = [
            'title' => '新增审批分类',
            'icon'  => 'fa fa-fw fa-plus',
            'class' => 'btn btn-info',
            'href'  => url('add'),
        ];
        $btn_access2 = [
            'title' => '删除审批分类',
            'icon'  => 'fa fa-fw fa-trash-o',
            'class' => 'btn btn-xs btn-default ajax-get confirm',
            'href'  => url('delete', ['id' => '__id__']),
        ];
        $processTypeRes = Db::name('qt_process_type')->where('pid','0')->column('name','id');
        $processTypeRes[0] = '顶级分类';
        //设置页面
        return ZBuilder::make('table')
            ->hideCheckbox()
            ->addColumn('id', '编号', 'text')
            ->addColumn('name', '审批分类', 'text')
            ->addColumn('sort', '排序', 'text')
            ->addColumn('pid', '上级分类', 'status','',$processTypeRes)
            ->addColumn('status', '状态', 'status','',['1'=>'显示','2'=>'隐藏'])
            ->addColumn('created_time', '创建日期', 'datetime','','Y-m-d H:i:s')
            ->addColumn('right_button','操作','btn')
            ->setColumnWidth([
                'id'  => 50,
                'created_time'=> 130,
                'right_button'=>80
            ])
            ->setTableName('qt_notic') // 指定数据表名
            ->addTopButton('custom',$btn_access) // 添加顶部按钮
            ->addRightButton('edit',$btn_access1) // 添加右侧按钮
            ->addRightButton('delete',$btn_access2) // 添加右侧按钮
            ->addOrder('id,title,create_time,update_time')
            ->setPrimaryKey('id') // 设置主键名为username
            ->setPageTitle('审批分类列表')
            ->setRowList($data_list) // 设置表格数据
            ->setPages($page) // 设置分页数据
            ->fetch();
    }
    //添加投票
    public function add(){
        //判断是否提交信息
        if ($this->request->isPost()) {
            // 表单数据
            $data = $this->request->post();
            $result = $this->validate($data, 'ProcessType.add');
            if(true !== $result) $this->error($result);
            $data['created_time'] = time();
            if (ProcessTypeModel::create($data)) {
                $this->success('新增成功', 'lists');
            } else {
                $this->error('新增失败');
            }
        }else {
            $processTypeRes = Db::name('qt_process_type')->where('pid','0')->column('name','id');
            $processTypeRes[0] = '顶级分类';
            return ZBuilder::make('form')
                ->addFormItems(
                    [
                        ['text:7','name','审批分类'],
                        ['select:7', 'pid', '状态', '',$processTypeRes,'0'],
                        ['radio', 'status', '状态', '', [1=>'显示', 2=>'隐藏'],'1'],
                        ['text:7','sort','排序','<code>值越小越靠前,1-100</code>','50'],
                    ]
                )
                ->setPageTitle('添加审批分类')
                ->fetch();
        }
    }

    //编辑审批分类
    public function edit($id=null){
        //判断是否提交信息
        if ($this->request->isPost()) {
            // 表单数据
            $data = $this->request->post();
            // 验证
            $result = $this->validate($data, 'ProcessType.add');
            if(true !== $result) $this->error($result);
            if (ProcessTypeModel::update($data)) {
                $this->success('编辑成功', 'lists');
            } else {
                $this->error('编辑失败');
            }
        }else
        {
            $info = ProcessTypeModel::get($id);
            $processTypeRes = Db::name('qt_process_type')->where('pid','0')->column('name','id');
            $processTypeRes[0] = '顶级分类';
            return ZBuilder::make('form')
                ->addFormItems(
                    [
                        ['hidden','id','id'],
                        ['text:7','name','审批分类'],
                        ['select:7', 'pid', '状态', '',$processTypeRes,'0'],
                        ['radio', 'status', '状态', '', [1=>'显示', 2=>'隐藏']],
                        ['text:7','sort','排序','<code>值越小越靠前,1-100</code>'],
                    ]
                )
                ->setFormData($info)
                ->setPageTitle('编辑公告')
                ->fetch();
        }
    }
    //删除审批分类
    public function delete($id=null){
        if ($id === null) $this->error('缺少参数');
        $find = Db::name('qt_process')->where('process_id',$id)->find();
        if($find){
            $this->error('已有审批流程的不允许删除');
        }
        try{
            Db::startTrans();
            //修改订单状态
            Db::name('qt_process_type')->where('id',$id)->delete();
            Db::commit();
        }catch (Exception $exception){
            Db::rollback();
            $this->error('系统繁忙请稍后再试','lists');
        }
        $this->success('删除成功','lists');

    }



}